package com.lbt.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbt.commonuntils.JwtUtils;
import com.lbt.commonuntils.R;
import com.lbt.commonuntils.ordervo.CourseWebVoOrder;
import com.lbt.eduservice.client.OrderClient;
import com.lbt.eduservice.entity.EduCourse;
import com.lbt.eduservice.entity.chapter.ChapterVo;
import com.lbt.eduservice.entity.front.CourseQueryVo;
import com.lbt.eduservice.entity.front.CourseWebVo;
import com.lbt.eduservice.service.EduChapterService;
import com.lbt.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin
@Api(description = "课程前端显示管理")
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {
    @Autowired
    EduCourseService courseService;

    @Autowired
    EduChapterService chapterService;

    @Autowired
    OrderClient orderClient;

    @ApiOperation("前端---课程查询带分页")
    @PostMapping(value = "getFrontCourseList/{page}/{limit}")
    public R getCourseList(@ApiParam(name = "page", value = "当前页码", required = true)
                               @PathVariable Long page,
                           @ApiParam(name = "limit", value = "每页记录数", required = true)
                               @PathVariable Long limit,
                           @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                               @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<EduCourse>coursePage=new Page<EduCourse>(page,limit);
        Map<String, Object> map=courseService.selectPageWeb(coursePage,courseQuery);
        return R.ok().data(map);
    }
    @ApiOperation("课程详情显示")
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId, HttpServletRequest request){
        //查询课程基本信息
        CourseWebVo courseWebVo=courseService.getBaseCourse(courseId);
        //查询课程章节和小节部分
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseID(courseId);

        //查询课程是否被购买
        boolean buyCourse=false;
        String token = JwtUtils.getMemberIdByJwtToken(request);
        if(token!=null){
            buyCourse = orderClient.isBuyCourse(courseId, token);
        }
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVoList).data("isBuy",buyCourse);
    }

    //远程调用 为订单做准备
    //根据课程ID  获取课程信息 献给公共类中增加一个返回的对象类
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourdeInfoOrder(@PathVariable("id") String id){
        CourseWebVo course = courseService.getBaseCourse(id);
        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(course,courseWebVoOrder);
        return courseWebVoOrder;
    }


}
